from utils import window
from bd2net import client
import sys


def main():
    try:
        info = window.win_init()
        if not info[0]:
            return

        def recvMsg(msg, obj):
            if msg['type'] == 'message':
                # noinspection PyArgumentList
                calls[2](msg, info[2])
            if msg['type'] == 'activeCount':
                # noinspection PyArgumentList
                calls[3](msg)

        def sendMsg(msg, name=info[2]):
            c.sendMsg({'type': 'message', 'content': msg, 'name': name})

        def closeCall():
            c.sendMsg({'type': 'action', 'action': 'close', 'content': info[2]})

        c = client.Connection(info[1], 12588, recvMsg)
        c.sendMsg({'type': 'action', 'action': 'welcome', 'content': info[2]})
        calls = [sendMsg, closeCall]
        window.win_main(calls)
        c.close()
    except:
        window.win_err('{}{}{}'.format(sys.exc_info()[0], sys.exc_info()[1], sys.exc_info()[2]))


if __name__ == '__main__':
    main()


